package com.lh.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.api.dao.ShopSortMapper;
import com.lh.api.service.ShopSortService;
import com.lh.api.vo.SearchVo;
import com.lh.api.vo.SortVo;
import com.lh.common.entity.admin.Category;
import com.lh.common.enums.ColumnTypeEnum;
import com.lh.common.enums.ShopSortTypeEnum;
import com.lh.common.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FH
 * @ClassName: ShopSortServiceImpl
 * @Description: (类作用)
 * @date: 2020-06-30 9:43
 */
@Service
public class ShopSortServiceImpl extends ServiceImpl<ShopSortMapper, Category> implements ShopSortService {

    @Override
    public PageUtils queryShopSortList() {
        List<SortVo> searchVos = this.baseMapper.queryShopSortList();
        Map<Long, SortVo> map = new HashMap<>();
        Map<Long, SortVo> secondmap = new HashMap<>();

        for (SortVo sortVo : searchVos){
            if(sortVo.getSortParentId() == 0){
                map.put(sortVo.getId(), sortVo);
            }else{
                SortVo parent = map.get(sortVo.getSortParentId());
                if(parent == null ) {
                    parent =  secondmap.get(sortVo.getSortParentId());
                }
                if(parent.getSortParentId() == 0){
                    secondmap.put(sortVo.getId(),sortVo);
                }else{
                    List<SortVo> child = parent.getChildren();
                    if(child == null ) child = new ArrayList<>();
                    if(sortVo.getShop() !=0){
                        parent.setShop(1l);
                        child.add(sortVo);
                        parent.setChildren(child);
                    }
                    secondmap.put(sortVo.getSortParentId(), parent);
                }
            }
        }
        for (Long id : secondmap.keySet()){
            SortVo sortVo = secondmap.get(id);
            SortVo first = map.get(sortVo.getSortParentId());
            List<SortVo> child = first.getChildren();
            if(child == null ) child = new ArrayList<>();
            if(sortVo.getShop() ==0){
                SortVo three = new SortVo();
                three.setId(sortVo.getId());
                three.setSortParentId(sortVo.getId());
                three.setSortName(sortVo.getSortName());
                three.setSortPic(sortVo.getSortPic());
                List<SortVo> tem = new ArrayList<>();
                tem.add(three);
                sortVo.setChildren(tem);
            }else {
                child.add(sortVo);
                first.setChildren(child);
                if(sortVo.getShop() !=0){
                    first.setShop(1l);
                }
            }
            map.put(sortVo.getSortParentId(), first);
        }
        List<SortVo> result = new ArrayList<>();

        for (Long id : map.keySet()){
            SortVo first = map.get(id);
            if(first.getShop() !=0 && CollectionUtils.isEmpty(first.getChildren())){
                SortVo three = new SortVo();
                three.setId(first.getId());
                three.setSortParentId(first.getId());
                three.setSortName(first.getSortName());
                three.setSortPic(first.getSortPic());

                SortVo second = new SortVo();
                second.setId(first.getId());
                second.setSortParentId(first.getId());
                second.setSortName(first.getSortName());
                second.setSortPic(first.getSortPic());
                List<SortVo> tem = new ArrayList<>();
                tem.add(three);
                second.setChildren(tem);

                List<SortVo> tem1 = new ArrayList<>();
                tem1.add(second);
                first.setChildren(tem1);
            }
            if(first.getShop() !=0){
                result.add(first);
            }
        }
        return new PageUtils<SortVo>(result);
    }

    public Category getCategoryStatus(Long cid){
        return this.baseMapper.getCategoryStatus(cid);
    }

    private void addData(List<SearchVo> list) {
        list.forEach(searchVo -> {
            searchVo.setPercent(new BigDecimal(0));
            if(searchVo.getPortionNum() > 0 ){
                searchVo.setPercent(new BigDecimal(searchVo.getCurrenPortion() * 100).divide(new BigDecimal(searchVo.getPortionNum()), 2, BigDecimal.ROUND_HALF_UP));
            }
        });
    }

    @Override
    public PageUtils<SearchVo> getCategoryShopList(Long cid, ShopSortTypeEnum sortType, String qureyTime, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        String sortName = "create_time";
        String sortOrder = "DESC";
        if (ShopSortTypeEnum.PRICEDESC.equals(sortType)){
            sortName = "price";
            sortOrder = "DESC";
        }else if (ShopSortTypeEnum.PRICEASC.equals(sortType)){
            sortName = "price";
            sortOrder = "ASC";
        }

        List<SearchVo> searchVos = this.baseMapper.getCategoryShopList(cid, qureyTime, sortName, sortOrder);
        addData(searchVos);
        return new PageUtils<>(searchVos);
    }
}
